#### Pre-process: .fpp -> .f90 via Fypp

# Create a list of the files to be preprocessed
set(fppFiles
    stdlib_ascii.fypp
    stdlib_bitsets.fypp
    stdlib_bitsets_64.fypp
    stdlib_bitsets_large.fypp
    stdlib_hash_32bit.fypp 
    stdlib_hash_32bit_fnv.fypp
    stdlib_hash_32bit_nm.fypp 
    stdlib_hash_32bit_water.fypp 
    stdlib_hash_64bit.fypp 
    stdlib_hash_64bit_fnv.fypp 
    stdlib_hash_64bit_pengy.fypp 
    stdlib_hash_64bit_spookyv2.fypp 
    stdlib_io.fypp
    stdlib_io_npy.fypp
    stdlib_io_npy_load.fypp
    stdlib_io_npy_save.fypp
    stdlib_kinds.fypp
    stdlib_linalg.fypp
    stdlib_linalg_diag.fypp
    stdlib_linalg_outer_product.fypp
    stdlib_optval.fypp
    stdlib_selection.fypp
    stdlib_sorting.fypp
    stdlib_sorting_ord_sort.fypp
    stdlib_sorting_sort.fypp
    stdlib_sorting_sort_index.fypp
    stdlib_stats.fypp
    stdlib_stats_corr.fypp
    stdlib_stats_cov.fypp
    stdlib_stats_mean.fypp
    stdlib_stats_median.fypp
    stdlib_stats_moment.fypp
    stdlib_stats_moment_all.fypp
    stdlib_stats_moment_mask.fypp
    stdlib_stats_moment_scalar.fypp
    stdlib_stats_distribution_uniform.fypp
    stdlib_stats_distribution_normal.fypp
    stdlib_stats_distribution_exponential.fypp
    stdlib_stats_var.fypp
    stdlib_quadrature.fypp
    stdlib_quadrature_trapz.fypp
    stdlib_quadrature_simps.fypp
    stdlib_random.fypp
    stdlib_math.fypp
    stdlib_math_linspace.fypp
    stdlib_math_logspace.fypp
    stdlib_math_arange.fypp
    stdlib_math_is_close.fypp
    stdlib_math_all_close.fypp
    stdlib_math_diff.fypp
    stdlib_string_type.fypp
    stdlib_string_type_constructor.fypp
    stdlib_strings_to_string.fypp
    stdlib_strings.fypp
    stdlib_version.fypp
)


# Custom preprocessor flags
if(DEFINED CMAKE_MAXIMUM_RANK)
  set(fyppFlags "-DMAXRANK=${CMAKE_MAXIMUM_RANK}")
elseif(f03rank)
  set(fyppFlags)
else()
  set(fyppFlags "-DVERSION90")
endif()

list(
  APPEND fyppFlags
  "-DWITH_QP=$<BOOL:${WITH_QP}>"
  "-DWITH_XDP=$<BOOL:${WITH_XDP}>"
  "-DPROJECT_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}"
  "-DPROJECT_VERSION_MINOR=${PROJECT_VERSION_MINOR}"
  "-DPROJECT_VERSION_PATCH=${PROJECT_VERSION_PATCH}"
)

fypp_f90("${fyppFlags}" "${fppFiles}" outFiles)

set(SRC
    stdlib_array.f90
    stdlib_error.f90
    stdlib_logger.f90
    stdlib_system.F90
    stdlib_specialfunctions.f90
    stdlib_specialfunctions_legendre.f90
    stdlib_quadrature_gauss.f90
    stdlib_stringlist_type.f90
    ${outFiles}
)

add_library(${PROJECT_NAME} ${SRC})

set_target_properties(
  ${PROJECT_NAME}
  PROPERTIES
  POSITION_INDEPENDENT_CODE ON
  WINDOWS_EXPORT_ALL_SYMBOLS ON
)

if(CMAKE_Fortran_COMPILER_ID STREQUAL GNU AND CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 10.0)
  target_compile_options(
     ${PROJECT_NAME}
     PRIVATE
     $<$<COMPILE_LANGUAGE:Fortran>:-fno-range-check>
   )
endif()

set(LIB_MOD_DIR ${CMAKE_CURRENT_BINARY_DIR}/mod_files/)
# We need the module directory before we finish the configure stage since the
# build interface might resolve before the module directory is generated by CMake
if(NOT EXISTS "${LIB_MOD_DIR}")
  make_directory("${LIB_MOD_DIR}")
endif()

set_target_properties(${PROJECT_NAME} PROPERTIES
    Fortran_MODULE_DIRECTORY ${LIB_MOD_DIR})
target_include_directories(${PROJECT_NAME} PUBLIC
    $<BUILD_INTERFACE:${LIB_MOD_DIR}>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR}>
)

if(f18errorstop)
  target_sources(${PROJECT_NAME} PRIVATE f18estop.f90)
else()
  target_sources(${PROJECT_NAME} PRIVATE f08estop.f90)
endif()

if(BUILD_TESTING)
  enable_testing()
  add_subdirectory(tests)
endif()

install(TARGETS ${PROJECT_NAME}
        EXPORT ${PROJECT_NAME}-targets
        RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
        ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
        LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)
install(DIRECTORY ${LIB_MOD_DIR} DESTINATION "${CMAKE_INSTALL_MODULEDIR}")
